#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"
#include "fr_device_canfd.h"
#define TCP_RX_DATA_SHOW_MAX  50
static const char tcp_tx_data[] = "tcp send test data!";
static uint8_t tcp_rx_data[TCP_RX_DATA_SHOW_MAX];
static uint8_t tcp_rx_data_update;
extern void tcp_write_sent_data(uint8_t * data, uint32_t len) ;
uint16_t tcp_ip[5] = {192,168,31,91,8080};
extern void tcp_clent_connect(void);
extern void bt_pan_open(void);
extern void tcp_clent_disconnect(void);
static void event_handler(lv_event_t *e)
{
	lv_event_code_t event = lv_event_get_code(e);
	lv_obj_t *obj = lv_event_get_target(e);

	if(!lv_obj_is_valid(obj))
        return;
	if(event == LV_EVENT_KEY)
	{
		uint32_t *key = lv_event_get_param(e);
        printf("bt_pan key = %d",*key);
        switch(*key)
        {
        case KEY1_CODE:
			tcp_write_sent_data((uint8_t*)tcp_tx_data,strlen(tcp_tx_data));	 
             break;
        case KEY3_CODE:
            tcp_clent_connect();
            break;
		case KEY4_CODE:	
            bt_pan_open();	 
             break;
        case KEY5_CODE: //return
            tcp_clent_disconnect();
            user_jump_page(fr_list_page);
            break;
        }
		printf("key_code %d \n", *key);
	}
}


extern uint8_t tcp_get_connect_state(void);
extern uint8_t bt_get_connect_state(void);
extern uint8_t pan_get_open_state(void);

static void page_del_cb(lv_event_t *e)
{
	if(e->user_data!=NULL)
	{
		lv_timer_del(e->user_data);
        e->user_data = NULL;
	}
}
static uint8_t timer_sec;
static void refresh_timer_cb(lv_timer_t *t)
{
   lv_obj_t *parent = t->user_data;
   if(!lv_obj_is_valid(parent))
		return;

    timer_sec++;
    if(timer_sec>=20){
        printf("t");
        timer_sec = 0;

    }
	 
	lv_label_set_text_fmt(lv_obj_get_child(parent,1),"bt connect state:  %s",bt_get_connect_state()?"ture":"false");
	lv_label_set_text_fmt(lv_obj_get_child(parent,2),"net open state:  %s",pan_get_open_state()?"ture":"false");
	lv_label_set_text_fmt(lv_obj_get_child(parent,3),"tcp connect state:  %s",tcp_get_connect_state()?"ture":"false");
    lv_label_set_text_fmt(lv_obj_get_child(parent,6),"tcp server IP:%d:%d:%d:%d  %d",tcp_ip[0],tcp_ip[1],tcp_ip[2],tcp_ip[3],tcp_ip[4]);
    if(tcp_rx_data_update){
        tcp_rx_data_update = 0;
        lv_label_set_text_fmt(lv_obj_get_child(parent,5)," %s",tcp_rx_data);
    }
//	uint8_t can_data[64]={0};
//	dev_get_can_raw(can_data,64);
//	char buf[64*4]={0};
//    uint8_t disp_len = dev_get_can_count();
//    int i = 0;
//    for(i=0;i<disp_len;i++)
//    {
//        snprintf(&buf[i*3],sizeof(buf),"%02x ",can_data[i]);
//    }
//    for (;i<64;i++) {
//        snprintf(&buf[i*3],sizeof(buf),"   ");
//    }
//	lv_label_set_text(lv_obj_get_child(parent,5),buf);
}
void tcp_rec_show_update(uint8_t *data,uint32_t len)
{
    memset(tcp_rx_data,0,TCP_RX_DATA_SHOW_MAX);
    memcpy(tcp_rx_data,data,len<TCP_RX_DATA_SHOW_MAX?len:(TCP_RX_DATA_SHOW_MAX -1));
    tcp_rx_data_update = 1;
//     lv_obj_t *parent = parent_local->user_data;
//     printf("data1:%s",data);
//    if(!lv_obj_is_valid(parent))
// 		return;
//     printf("data2:%s",data);
//     lv_label_set_text_fmt(lv_obj_get_child(parent,5),"%s",data);
}

void fr_bt_pan_page(lv_obj_t* parent,lv_point_t *top)
{
	uint8_t i;
    if(!lv_obj_is_valid(parent))
        return;
    UI_PARENT_INIT(parent);


	lv_obj_t* tile_label = lv_label_create(parent);
    lv_obj_align(tile_label,LV_ALIGN_TOP_MID,0,10);
    lv_obj_set_style_text_font(tile_label,LV_FONT_ARIALUNI_ALL_32PX,0);
    lv_obj_set_style_text_color(tile_label,lv_color_make(0x0, 0xff, 0x0),0);
    lv_label_set_text(tile_label,"蓝牙上网 测试");


    lv_obj_t* bt_state_label = lv_label_create(parent);
    lv_obj_set_style_text_font(bt_state_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(bt_state_label,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text_fmt(bt_state_label,"bt connect state:%s",bt_get_connect_state()?"ture":"false");
    lv_obj_align(bt_state_label,LV_ALIGN_TOP_LEFT,10,80);

    lv_obj_t* pan_state_label = lv_label_create(parent);
    lv_obj_set_style_text_font(pan_state_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(pan_state_label,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text_fmt(pan_state_label,"net open state:%s",pan_get_open_state()?"ture":"false");
    lv_obj_align(pan_state_label,LV_ALIGN_TOP_LEFT,10,100);

    lv_obj_t* tcp_state_label = lv_label_create(parent);
    lv_obj_set_style_text_font(tcp_state_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(tcp_state_label,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text_fmt(tcp_state_label,"tcp connect state:%s",tcp_get_connect_state()?"ture":"false");
    lv_obj_align(tcp_state_label,LV_ALIGN_TOP_LEFT,10,120);


    lv_obj_t* data_tile = lv_label_create(parent);
	lv_obj_set_style_text_font(data_tile,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(data_tile,lv_color_make(0xff, 0xff, 0xff),0);
    lv_label_set_text(data_tile,"rx data:");
    lv_obj_align(data_tile,LV_ALIGN_TOP_LEFT,10,160);
 
	
	lv_obj_t* data_label = lv_label_create(parent);
	lv_obj_set_style_text_font(data_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(data_label,lv_color_make(0xff, 0xff, 0xff),0);
	lv_obj_set_pos(data_label,10,180);
	lv_label_set_text(data_label,"receiving data");
	lv_obj_set_width(data_label,LV_PCT(60));

    lv_obj_t* tcp_ip_label = lv_label_create(parent);
	lv_obj_set_style_text_font(tcp_ip_label,LV_FONT_SMALL_NUMBER_20PX,0);
    lv_obj_set_style_text_color(tcp_ip_label,lv_color_make(0xff, 0, 0),0);
	lv_obj_set_pos(tcp_ip_label,10,60);
	lv_label_set_text(tcp_ip_label," ");
	lv_obj_set_width(tcp_ip_label,LV_PCT(100));
	
	lv_timer_t *refresh_timer = lv_timer_create(refresh_timer_cb,50,parent);
	
	lv_obj_add_event_cb(data_label,page_del_cb,LV_EVENT_DELETE,(void*)refresh_timer);
	
	lv_obj_add_event_cb(parent,event_handler, LV_EVENT_KEY, NULL);
	
	
}
